////
//
/// \file ./test/label_node_test.cpp
//
//  Copyright 2007 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/label_node.hpp>

#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

TEST(LabelNodeTest, basic_parse)
{
  const string data[] = { "foo",
                          "foo_bar",
                          "foo42",
                          "foo_42bar",
                          "__foobar",
                          "__bar__" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    LabelNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i], string( data[i].begin(), last_parsed ) );
    EXPECT_EQ( data[i], node.get_text() );
  }
}

TEST(LabelNodeTest, padded_parse)
{
  const string data[] = { "  foo  ", "\t\tbar\t\t" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    LabelNode node;
    string::const_iterator last_parsed;
    string::const_iterator label_first =
        data[i].begin() + data[i].find_first_not_of( " \t" );
    string::const_iterator label_last =
        data[i].begin() + data[i].find_last_not_of( " \t" ) + 1;
    EXPECT_FALSE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Erroneously parsed: '" << data[i] << "'";
    EXPECT_FALSE( node.parse( ++data[i].begin(), data[i].end(), last_parsed ) )
        << "Erroneously parsed: '" << data[i].substr( 1 ) << "'";
    EXPECT_TRUE( node.parse( label_first, data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( string( label_first, label_last ),
               string( label_first, last_parsed ) );
    EXPECT_EQ( string( label_first, label_last ), node.get_text() );
  }
}

TEST(LabelNodeTest, parse_fail)
{
  const string data[] = { "", "_", "10",  //  malformed
                          "begin", "end", //  reserved
                          "if", "then", "else",
                          "loop", "pre", "post",
                          "break", "broken", "finish", "finished", };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    LabelNode node;
    string::const_iterator last_parsed = data[i].end();
    EXPECT_FALSE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Erroneously parsed: '" << data[i] << "'";
    EXPECT_EQ( string(), string( data[i].begin(), last_parsed ) );
  }
}

} //  end parser namespace
} //  end inc namespace
